package mouseevent;
/**
 * 鼠标事件使用三类监听器
 * 1.addMouseListener  点击，按下，抬起，移入，移除
 * 2.addMouseMotionListener  移动，拖动
 * 3.addMouseWheelListener  鼠标滚轮转动
 *
 * 鼠标事件的参数
 * - getPoint()/getX/ getY():点击的坐标，相对于该控件
 * - getPointOnScreen()/ getXOnScreen() / getYOnScreen()：屏幕坐标
 * - getSource():事件源，即点中的控件
 * - getButton()：左键、中键、右键
 * - getClickCount():单击、双击、三击
 */

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;

public class MyDemo {
    public static void main(String[] args) {


        JFrame jFrame = new JFrame("java-swing");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(400,300);
        jFrame.setVisible(true);


        JPanel jPanel = new JPanel();
        jFrame.setContentPane(jPanel);

        JButton button = new JButton("button");
        jPanel.add(button);


        //addMouseListener
        button.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("mouseClicked");
            }

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("mousePressed");
                if (e.getButton()==MouseEvent.BUTTON1){
                    System.out.println("左键");
                }
                if (e.getButton()==MouseEvent.BUTTON2){
                    System.out.println("中键");
                }
                if (e.getButton()==MouseEvent.BUTTON3){
                    System.out.println("右键");
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("mouseReleased");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("mouseEntered");
                button.setText("mouseEntered");

            }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("mouseExited");
                button.setText("mouseExited");

            }
        });

        button.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                System.out.println("mouseDragged");
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("mouseMoved");

            }
        });


    }
}
